﻿@using System.Diagnostics
@using System.Globalization
@using System.Web.Hosting
@using MvcForum.Core.Constants
@using MvcForum.Core.Interfaces.Services
@using MvcForum.Web.Application

@{
    // Get the current theme
    var currentTheme = DependencyResolver.Current.GetService<ISettingsService>().GetSettings().Theme;

    // Set if this is a mobile device
    PageData[Constants.IsMobileDevice] = Context.IsMobileDevice();

    // Set the Layout view
    //const string layoutName = "_Layout";
    //Layout = string.Concat("~/Themes/", currentTheme, "/Views/Shared/", layoutName, ".cshtml");

    const string layoutName = "_Layout";
    Layout = "~/Views/Shared/" + layoutName + ".cshtml";

    // set the theme locations as valid locations for views
    var locationFormats = new List<string>
    {
        string.Concat("~/Themes/", currentTheme, "/Views/{0}.cshtml"),
        string.Concat("~/Themes/", currentTheme, "/Views/Shared/{0}.cshtml")
    };

    foreach (var format in locationFormats)
    {
        var virtualPath = string.Format(
            CultureInfo.InvariantCulture,
            format,
            layoutName);
        Trace.WriteLine(virtualPath);
        if (!HostingEnvironment.VirtualPathProvider.FileExists(virtualPath))
        {
            continue;
        }

        Layout = virtualPath;
        break;
    }
}